#include "sprite.h"
#include "trace.h"
#include "picture.h"
#include <d3dx9.h>

int Colum[3] = {0,52,122};

CSprite::CSprite(LPD3DXSPRITE SpriteHandler, LPWSTR FilePath) 
{
	D3DXIMAGE_INFO info; 
	HRESULT result; 
	
	_Image = NULL;
	_SpriteHandler = SpriteHandler;

	result = D3DXGetImageInfoFromFile(FilePath,&info);
	if (result!=D3D_OK)
	{
		trace(L"[ERROR] Failed to get information from image file '%s'",FilePath);
		return;
	}

	LPDIRECT3DDEVICE9 d3ddv; 
	SpriteHandler->GetDevice(&d3ddv);

	result = D3DXCreateTextureFromFileEx(
		d3ddv,
		FilePath,
		info.Width,
		info.Height,
		1,
		D3DUSAGE_DYNAMIC,
		D3DFMT_UNKNOWN,
		D3DPOOL_DEFAULT,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(92,148,252),
		&info,
		NULL,
		&_Image);

	if (result!=D3D_OK) 
	{
		trace(L"[ERROR] Failed to create texture from file '%s'",FilePath);
		return;
	}
	trace(L"Load successful '%s'",FilePath);
}

void CSprite::Render(LPDIRECT3DSURFACE9 Target, float X, float Y,int w, int h,int lv, int anim, int movingWay,int blend)
{
	RECT srect;
	srect.top = Colum[lv];
	srect.bottom = srect.top + h;
	if (lv == 2)
	{
		if(movingWay == 0)
		{
			srect.left = (7-anim)*(26)-(5-anim);
			if(anim == 0) srect.left += 3;
		}
		else
		{
			srect.left = (8+anim)*(26)+(4-anim);
			if(anim == 0) srect.left -= 3;
		}
	}
	else
	{
		if(movingWay == 0)
			srect.left = (6-anim)*(30);
		else
			srect.left = (7+anim)*(30)-1;
	}
	srect.right = srect.left + w;

	if(lv == -1)
	{
		srect.left = 0;
		srect.right = srect.left+w;
		srect.top = 16;
		srect.bottom = srect.top + h;
	}

	D3DXVECTOR3 position(X-scr_x,Y-h-scr_y,0);

	if(blend == 0)
	{
		_SpriteHandler->Draw(
			_Image,
			&srect,
			NULL,
			&position, 
			D3DCOLOR_XRGB(255,255,255)
		);
	}
	else if(blend == 1)
	{
		_SpriteHandler->Draw(
			_Image,
			&srect,
			NULL,
			&position, 
			D3DCOLOR_XRGB(255,0,0)
		);	
	}
	else if(blend == 2)
	{
		_SpriteHandler->Draw(
			_Image,
			&srect,
			NULL,
			&position, 
			D3DCOLOR_XRGB(0,0,0)
		);	
	}
}

void CSprite::Render(LPDIRECT3DSURFACE9 Target, float X, float Y,int Row, int Col)
{
	RECT srect;
	srect.top = 17*Row;
	srect.bottom = srect.top + 16;
	srect.left = 17*Col;
	srect.right = srect.left + 16;
	
	D3DXVECTOR3 position(X-scr_x,Y-16-scr_y,0);

	_SpriteHandler->Draw(
		_Image,
		&srect,
		NULL,
		&position, 
		D3DCOLOR_XRGB(255,255,255)
	);
}

void CSprite::Render(LPDIRECT3DSURFACE9 Target, float X, float Y, int w, int h, int Col)
{
	RECT srect;
	if (Col!=4)
		srect.top = 4;
	else 
		srect.top = 0;
	srect.bottom = srect.top + h;
	if (Col <= 4)
		srect.left = 14*Col;
	else 
		srect.left = 14*Col+4;
	srect.right = srect.left + w;
	
	D3DXVECTOR3 position(X-scr_x,Y-h-scr_y,0);

	_SpriteHandler->Draw(
		_Image,
		&srect,
		NULL,
		&position, 
		D3DCOLOR_XRGB(255,255,255)
	);
}

void CSprite::Render(LPDIRECT3DSURFACE9 Target, float X,  float Y, int w, int h, int R, int C)
{
	RECT srect;
	srect.top = 30*R;
	srect.bottom = srect.top + h;
	srect.left = (w+14)*C;
	srect.right = srect.left + w;
	
	D3DXVECTOR3 position(X-scr_x,Y-h-scr_y,0);

	_SpriteHandler->Draw(
		_Image,
		&srect,
		NULL,
		&position, 
		D3DCOLOR_XRGB(255,255,255)
	);
}

void CSprite::Render(LPDIRECT3DSURFACE9 Target, float X, float Y, int num)
{
	RECT srect;
	srect.top = 0;
	srect.bottom = 16;
	srect.left = num*18;
	srect.right = num*18+18;
	
	D3DXVECTOR3 position(X,Y,0);

	_SpriteHandler->Draw(
		_Image,
		&srect,
		NULL,
		&position, 
		D3DCOLOR_XRGB(255,255,255)
	);

}

void CSprite::Render(LPDIRECT3DSURFACE9 Target, int x, int y)
{
	
	D3DXVECTOR3 position(x,y,0);

	_SpriteHandler->Draw(
		_Image,
		NULL,
		NULL,
		&position, 
		D3DCOLOR_XRGB(255,255,255)
	);
}

void CSprite::Render(LPDIRECT3DSURFACE9 Target)
{
	RECT srect;
	srect.top = 0;
	srect.bottom = 46;
	srect.left = 0;
	srect.right = 217;
	
	D3DXVECTOR3 position(16,16,0);

	_SpriteHandler->Draw(
		_Image,
		&srect,
		NULL,
		&position, 
		D3DCOLOR_XRGB(255,255,255)
	);
}

CSprite::~CSprite()
{
	_Image->Release();
}